package it.patterns.structural.facade;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * 
 * Provide a unified interface to a set of interfaces in a subsystem. Facade
 * defines a higher level interface that makes the subsystem easier to use. You
 * can provide a simple interface to a more complex subsystem.
 * 
 * Using Facade you can decouple the subsystem from clients and other subsystem,
 * thereby promoting subsystem independence and portability.
 * 
 * Clients communicate with the subsystem by sending requests to Facade, which
 * forwards them to the appropriate subsystem object(s). Clients that use the
 * Facade don't have to access its subsystem directly.
 * 
 * Advatanges:
 * 
 * <ul>
 * <li>Reduce the number of objects that the clients deal with.</li>
 * <li>Make the subsystem easier to use</li>
 * <li>Weak coupling between clients and subsystem: often the component in a
 * subsystem are strongly coupled</li>
 * </ul>
 * 
 * 
 * <img src="Facade.jpg" alt="Pattern Structure"/>
 * 
 * @author Luca D'Onofrio
 * 
 * @category Structural Pattern
 * @see "Design Patterns - Element of Reusable Object Oriented Software (Gamma,Helm, Johnson, Vlissides)"
 * 
 * @version 1.0
 */
public class Facade {
	public static final boolean SECURE_MODE_ON = true;
	public static final boolean SECURE_MODE_OFF = false;
	BufferedReader br;
	boolean secureMode;

	public Facade() {
		this(SECURE_MODE_ON);
	}

	public Facade(boolean secMod) {
		br = new BufferedReader(new InputStreamReader(System.in));
		secureMode = secMod;
	}

	public String readString() throws Exception {
		try {
			return br.readLine();
		} catch (Exception e) {
			if (secureMode)
				throw new Exception(e);
			return "";
		}
	}

	public Integer readInteger() throws Exception {
		Integer theInteger = null;
		try {
			theInteger = new Integer(br.readLine());
		} catch (Exception e) {
			if (secureMode)

				throw new Exception(e);
			theInteger = new Integer(0);
		}
		return theInteger;
	}

	public Double readDouble() throws Exception {
		Double theDouble = null;
		try {
			theDouble = new Double(br.readLine());
		} catch (Exception e) {
			if (secureMode)
				throw new Exception(e);
			theDouble = new Double(0);
		}
		return theDouble;
	}
}
